/**
 *====================================================
 * 文件名称: NewsActionApiImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年9月21日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.news.action.v1;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.news.action.v1.dto.NewsCastor;
import com.yuexunit.finance.news.action.v1.model.NewsDetail;
import com.yuexunit.finance.news.action.v1.model.NewsSummary;
import com.yuexunit.finance.news.entity.NewsContentEntity;
import com.yuexunit.finance.news.entity.NewsEntity;
import com.yuexunit.finance.news.service.NewsService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;

/**
 * @ClassName: NewsActionApiImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月21日 下午7:59:44
 */
@Implements(NewsActionApi.class)
public class NewsActionApiImpl implements NewsActionApi {

	private NewsService newsService = ComponentFactory.loadClass(NewsService.class);

	private static NewsEntity createNewsEntity(Long newsCategoryId, String newsTitle, String summary, String recommend, String imageUuid, String newsSource, String newsSourceLink, String[] newsContentIds, String[] contents,
			String[] fileUuids) {
		NewsEntity newsEntity = new NewsEntity();
		newsEntity.setNewsCategoryId(newsCategoryId);
		newsEntity.setNewsTitle(newsTitle);
		newsEntity.setSummary(summary);
		newsEntity.setRecommend(BooleanUtils.toBoolean(recommend));
		newsEntity.setImageUuid(imageUuid);
		newsEntity.setNewsSource(newsSource);
		newsEntity.setNewsSourceLink(newsSourceLink);
		newsEntity.setPublisherId(FinanceSessionContext.getAccountId());
		newsEntity.setPublisher(FinanceSessionContext.getAccountFullName());

		if (contents != null && contents.length > 0) {
			List<NewsContentEntity> contentEntities = new ArrayList<>();
			for (int i = 0; i < contents.length; i++) {
				NewsContentEntity contentEntity = new NewsContentEntity();
				contentEntity.setNewsCategoryId(newsCategoryId);
				contentEntity.setContent(contents[i]);
				if (StringUtils.isBlank(newsEntity.getSummary()) && StringUtils.isNotBlank(contents[i])) {
					newsEntity.setSummary(contents[i]);
				}
				if (StringUtils.isBlank(newsEntity.getImageUuid()) && StringUtils.isNotBlank(fileUuids[i])) {
					newsEntity.setImageUuid(fileUuids[i]);
				}
				contentEntity.setFileUuid(fileUuids[i]);
				contentEntity.setSequence(i);
				if (newsContentIds != null && StringUtils.isNotBlank(newsContentIds[i])) {
					contentEntity.setNewsContentId(Long.parseLong(newsContentIds[i]));
				}
				contentEntities.add(contentEntity);
			}
			newsEntity.setContents(contentEntities);
		}
		return newsEntity;
	}

	@Override
	public String createNews(Long newsCategoryId, String newsTitle, String summary, String rerecommend, String imageUuid, String newsSource, String newsSourceLink, String[] contents, String[] fileUuids) {
		NewsEntity newsEntity = createNewsEntity(newsCategoryId, newsTitle, summary, rerecommend, imageUuid, newsSource, newsSourceLink, null, contents, fileUuids);
		return String.valueOf(newsService.createNews(newsEntity));
	}

	@Override
	public void updateNews(Long newsId, Long newsCategoryId, String newsTitle, String summary, String recommend, String imageUuid, String newsSource, String newsSourceLink, String[] newsContentIds, String[] contents, String[] fileUuids) {
		NewsEntity newsEntity = createNewsEntity(newsCategoryId, newsTitle, summary, recommend, imageUuid, newsSource, newsSourceLink, newsContentIds, contents, fileUuids);
		newsService.updateNews(newsEntity);
	}

	@Override
	public void disableNews(Long newsId) {
		newsService.disableNews(newsId);
	}

	@Override
	public void recommendNews(Long newsId) {
		newsService.recommendNews(newsId);
	}

	@Override
	public void disableRecommendNews(Long newsId) {
		newsService.disableRecommendNews(newsId);
	}

	@Override
	public NewsDetail inquireNewsDetail(Long newsId) {
		NewsEntity entity = newsService.inquireNewsDetail(newsId);
		return NewsCastor.detailFromNewsEntity(entity);
	}

	@Override
	public PageList<NewsSummary> inquireNewsPageList(Long newsCategoryId, Integer recommend, String newsTitle, String publisher, String issueDateFromTime, String issueDateToTime, PageCriteria page) {
		Boolean recommendFlag = recommend != null ? BooleanUtils.toBoolean(recommend) : null;
		PageList<NewsEntity> pageList = newsService.inquireNewsPageList(newsCategoryId, recommendFlag, newsTitle, publisher, page, issueDateFromTime, issueDateToTime, false);
		PageList<NewsSummary> list = new PageList<NewsSummary>(page, pageList.getTotalRecordCount());
		for (NewsEntity entity : pageList) {
			NewsSummary newsSummary = NewsCastor.summaryFromNewsEntity(entity);
			list.add(newsSummary);
		}
		return list;
	}

	@Override
	public PageList<NewsSummary> inquireHelpPageList(String content, PageCriteria page) {
		PageList<NewsEntity> pages = newsService.inquireHelpPageList(content, page);
		PageList<NewsSummary> results = new PageList<NewsSummary>(page, pages.getTotalRecordCount());
		for (NewsEntity entity : pages) {
			NewsSummary newsSummary = NewsCastor.summaryFromNewsEntity(entity);
			results.add(newsSummary);
		}
		return results;
	}

	@Override
	public PageList<NewsSummary> inquireAllNewsPageList(Long newsCategoryId, Integer recommend, String newsTitle, String publisher, String issueDateFromTime, String issueDateToTime, PageCriteria page) {
		Boolean recommendFlag = recommend != null ? BooleanUtils.toBoolean(recommend) : null;
		PageList<NewsEntity> pageList = newsService.inquireNewsPageList(newsCategoryId, recommendFlag, newsTitle, publisher, page, issueDateFromTime, issueDateToTime, true);
		PageList<NewsSummary> list = new PageList<NewsSummary>(page, pageList.getTotalRecordCount());
		for (NewsEntity entity : pageList) {
			NewsSummary newsSummary = NewsCastor.summaryFromNewsEntity(entity);
			list.add(newsSummary);
		}
		return list;
	}

}
